﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TTApp.Phone.Silverlight.Hexagons
{
    /// <summary>
    /// Bảng kết quả được hiển thị sau khi kết của mỗi game.
    /// </summary>
    public partial class ResultBoard : UserControl
    {
        #region //--------- Private Fields --------------//
        private GameBase _game;
        private Gameplay _gameplay;
        #endregion

        #region //--------- Public Constructors ---------//
        /// <summary>
        /// Khởi tạo đối tượng.
        /// </summary>
        public ResultBoard()
        {
            InitializeComponent();
        }
        #endregion

        #region //--------- CLR Properties --------------//
        public GameBase Game
        {
            get { return _game; }
            set { _game = value; }
        }

        public Gameplay Gameplay
        {
            get { return _gameplay; }
            set { _gameplay = value; }
        }

        /// <summary>
        /// Khi hiển thị, xem ván chơi vừa rồi là thắng hay hòa để hiển thị ảnh tương ứng.
        /// </summary>
        public new Visibility Visibility
        {
            get
            {
                return base.Visibility;
            }
            set
            {
                base.Visibility = value;
                if (base.Visibility == Visibility.Visible)
                {
                    GameState gameState = _game.CheckGameState();
                    if (gameState == GameState.Draw)
                    {
                        background.Source = new ImageSourceConverter().ConvertFrom("/TTApp.Phone.Silverlight;component/Assets/Images/Draw.png") as ImageSource;
                        p1RestartRect.Visibility = p1BackToMenuRect.Visibility = Visibility.Visible;
                        p2RestartRect.Visibility = p2BackToMenuRect.Visibility = Visibility.Collapsed;
                    }
                    else if (gameState == GameState.Win)
                    {
                        if (_game.GetVictoryPlayer() == _game.GetPlayer(1))
                        {
                            if (_game.GetType() == typeof(ClassicGame))
                            {
                                background.Source = new ImageSourceConverter().ConvertFrom("/TTApp.Phone.Silverlight;component/Assets/Images/Player1Win.png") as ImageSource;
                                p1RestartRect.Visibility = p1BackToMenuRect.Visibility = Visibility.Visible;
                                p2RestartRect.Visibility = p2BackToMenuRect.Visibility = Visibility.Collapsed;   
                            }
                            else if (_game.GetType() == typeof(AIClassicGame))
                            {
                                background.Source = new ImageSourceConverter().ConvertFrom("/TTApp.Phone.Silverlight;component/Assets/Images/YouWin.png") as ImageSource;
                                p1RestartRect.Visibility = p1BackToMenuRect.Visibility = Visibility.Visible;
                                p2RestartRect.Visibility = p2BackToMenuRect.Visibility = Visibility.Collapsed;   
                            }
                        }
                        else if (_game.GetVictoryPlayer() == _game.GetPlayer(2))
                        {
                            if (_game.GetType() == typeof(ClassicGame))
                            {
                                background.Source = new ImageSourceConverter().ConvertFrom("/TTApp.Phone.Silverlight;component/Assets/Images/Player2Win.png") as ImageSource;
                                p1RestartRect.Visibility = p1BackToMenuRect.Visibility = Visibility.Collapsed;
                                p2RestartRect.Visibility = p2BackToMenuRect.Visibility = Visibility.Visible;
                            }
                            else if (_game.GetType() == typeof(AIClassicGame))
                            {
                                background.Source = new ImageSourceConverter().ConvertFrom("/TTApp.Phone.Silverlight;component/Assets/Images/AIWin.png") as ImageSource;
                                p1RestartRect.Visibility = p1BackToMenuRect.Visibility = Visibility.Visible;
                                p2RestartRect.Visibility = p2BackToMenuRect.Visibility = Visibility.Collapsed;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region //--------- Event Handlers --------------//
        private void OnBackToMenuRectTap(object sender, GestureEventArgs args)
        {
            _game.Gameplay.Page.NavigationService.GoBack();   
        }

        private void OnRestartRectTap(object sender, GestureEventArgs args)
        {
            _game.RestartGame();
        }
        #endregion
    }
}
